#!/user/bin/env python3
# -*- coding: utf-8 -*-
"""
--------------------------------------
    Author:     JiChao_Song
    Date  :     2021/11/30 12:44
    Desc  :
--------------------------------------
"""
from django.core.cache import cache

from common.utils.log_utils import logger
from platform_auth.settings import EXPIRE_TIMEOUT


def save_token(userId: str, data: dict, timeout=None, request=None):
    if timeout is None:
        timeout = EXPIRE_TIMEOUT

    appName = 'PLATFORM_AUTH'

    return cache.set(
        key = f'{appName.upper()}.{userId}',
        value = data,
        timeout = timeout
    )


def save_token_appid(userId: str, data: dict, timeout=None):
    if timeout is None:
        timeout = EXPIRE_TIMEOUT

    appName = 'PLATFORM_AUTH'

    return cache.set(
        key = f'{appName.upper()}.{userId}',
        value = data,
        timeout = timeout
    )


def get_token(userId, token):
    appName = 'PLATFORM_AUTH'

    payload = cache.get(f'{appName.upper()}.{userId}')
    flag = True
    logger.info(f'payload: {appName.upper()}.{userId}')
    if payload:
        if payload.get('token') != token and token is not None:
            flag = False
    else:
        flag = False
    logger.info(f'flag {flag}')
    return flag


def delete_token(userId):
    appName = 'PLATFORM_AUTH'

    cache.delete(f'{appName.upper()}.{userId}')
